package com.example.test.dialogfactory;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

/**
 * ViewModel共享实例,通过相同的ViewModelStoreOwner,所有activity共享相同的ViewModelStoreOwner
 */
public class MainActivity113 extends AppCompatActivity {

    private static final String TAG = "MainActivity113";

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity113.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main113 );

        AndroidViewModel viewModel =
            new ViewModelProvider( this, ViewModelFactory.getInstance( this ) ).get( AndroidViewModel.class );
        Log.i( TAG, "onCreate: " + viewModel );

        getSupportFragmentManager().beginTransaction().replace( R.id.container0, new Fragment0() ).commit();
        getSupportFragmentManager().beginTransaction().replace( R.id.container1, new Fragment1() ).commit();
    }

    public static class Fragment0 extends Fragment {

        @Override
        public void onCreate( @Nullable Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );
            /*相同的ViewModelStoreOwner,获取的ViewModel相同*/
            ViewModelProvider viewModelProvider = new ViewModelProvider( getActivity(),
                ViewModelFactory.getInstance( getContext() ) );
            AndroidViewModel androidViewModel = viewModelProvider.get( AndroidViewModel.class );
            Log.i( TAG, "onCreateA: " + androidViewModel );
        }
    }

    public static class Fragment1 extends Fragment {

        @Override
        public void onCreate( @Nullable Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );
            /*相同的ViewModelStoreOwner,获取的ViewModel相同*/
            ViewModelProvider viewModelProvider = new ViewModelProvider( getActivity(),
                ViewModelFactory.getInstance( getContext() ) );
            AndroidViewModel androidViewModel = viewModelProvider.get( AndroidViewModel.class );
            Log.i( TAG, "onCreateB: " + androidViewModel );
        }
    }

    public static class AndroidViewModel extends ViewModel {

        private Application mApplication;

        public AndroidViewModel( Application application ) {

            mApplication = application;
        }

        public Application getApplication() {

            return mApplication;
        }
    }

    public static class ViewModelFactory implements ViewModelProvider.Factory {

        public static ViewModelFactory instance = null;

        public static ViewModelFactory getInstance( Context context ) {

            if ( instance == null ) {
                instance = new ViewModelFactory( ((Application) context.getApplicationContext()) );
            }
            return instance;
        }

        private Application mApplication;

        public ViewModelFactory( Application application ) {

            mApplication = application;
        }

        @NonNull
        @Override
        public < T extends ViewModel > T create( @NonNull Class< T > modelClass ) {

            if ( modelClass == AndroidViewModel.class ) {
                return (T) new AndroidViewModel( mApplication );
            }
            return null;
        }
    }
}